import axios, { AxiosRequestConfig, AxiosResponse,Method } from "axios";
// import { ElLoading } from 'element-plus';

// 加载效果
// let loading:any
// const startLoad = () => {
//     interface Options {
//         lock: boolean;
//         text: string;
//         background: string;
//     }
//     const options:Options = {
//         lock: true,
//         text: '加载中',
//         background:'rgba(0,0,0,.7)'
//     }
//     loading = ElLoading.service(options);
// }

// // 关闭加载效果
// const endLoad = () => {
//     loading.close()
// }
interface PendingType {
    url: string | undefined;
    method: Method | undefined;
    params: object;
    data: object;
    cancel: Function;
}

// 取消重复请求
const pending: Array<PendingType> = [];
const CancelToken = axios.CancelToken;

// 移除重复请求
const removePending = (config: AxiosRequestConfig) => {
    for (const key in pending) {
        const item: number = +key;  //+key是数字类型 key是字符串类型
        const list: PendingType = pending[key];
        // 当前请求在数组中存在时执行函数体
        if (list.url === config.url && list.method === config.method && JSON.stringify(list.params) === JSON.stringify(config.params) && JSON.stringify(list.data) === JSON.stringify(config.data)) {
            // 执行取消操作
            list.cancel('操作太频繁，请稍后再试');
            // 从数组中移除记录
            pending.splice(item, 1);
        }
    }
};
const server = axios.create({
    baseURL:""
})
// 请求拦截
server.interceptors.request.use((config: AxiosRequestConfig) => {
    // startLoad()
    removePending(config);
    config.cancelToken = new CancelToken((c) => {
        pending.push({ url: config.url, method: config.method, params: config.params, data: config.data, cancel: c });
    });
    return config
}, err => { 
        
})
server.defaults.headers.post['Content-Type'] = 'application/json'
// 响应拦截
server.interceptors.response.use((response: AxiosResponse) => {
    // endLoad()
    removePending(response.config);
    return response
}, err => { 
    // endLoad()
    return Promise.reject(err)
})

// 封装get post put delete请求方法
const http = {
    get:function(url:string,params:any) { 
        return new Promise((resolve, reject) => { 
            server.get(url, { params: params} ).then(res => { 
                resolve(res)
            }).catch(err => { 
                reject(err)
            })
        })
    },
    post:function(url:string,data:any) { 
        return new Promise((resolve, reject) => { 
            server.post(url, data ).then(res => { 
                resolve(res)
            }).catch(err => { 
                reject(err)
            })
        })
    },
    put:function(url:string,data:any) { 
        return new Promise((resolve, reject) => { 
            server.put(url, data ).then(res => { 
                resolve(res)
            }).catch(err => { 
                reject(err)
            })
        })
    },
    delete:function(url:string,params:any) { 
        return new Promise((resolve, reject) => { 
            server.delete(url, {params:params} ).then(res => { 
                resolve(res)
            }).catch(err => { 
                reject(err)
            })
        })
    },
}
export default http